//
//  JWPosterMovieCell.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/22.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "JWPosterMovieCell.h"
#import "UIImageView+WebCache.h"
#import "UIImage+MultiFormat.h"
#import "JWCustomButton.h"
#import "JWMovieCellModel.h"

@interface JWPosterMovieCell ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *firstBtn;
@property (nonatomic, strong) JWCustomButton *secondBtn;
@property (nonatomic, strong) UIImage *image;

@end
@implementation JWPosterMovieCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {

        UIButton *firstBtn = [[UIButton alloc] initWithFrame:self.bounds];
        firstBtn.backgroundColor = [UIColor whiteColor];
        _firstBtn = firstBtn;
        _firstBtn.tag = 1;
        [_firstBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        JWCustomButton *secondBtn = [[JWCustomButton alloc] initWithFrame:self.bounds];
        secondBtn.backgroundColor = [UIColor grayColor];
        _secondBtn = secondBtn;
        _secondBtn.tag = 0;
        [_secondBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
 
        [self addSubview:secondBtn];
        [self addSubview:firstBtn];
    }
    return self;
}

- (void)btnAction:(UIButton *)btn{
    self.isFirst = btn.tag;
}

- (void)setIsFirst:(BOOL)isFirst{
    _isFirst = isFirst;
    if (_isFirst == YES) {
        [UIView transitionFromView:self.firstBtn toView:self.secondBtn duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
    }
    if (_isFirst == NO){
        [UIView transitionFromView:self.secondBtn toView:self.firstBtn duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
    }
    
    
}

- (void)setMovieModel:(JWMovieCellModel *)movieModel{
    _movieModel = movieModel;
    
    
    NSString *urlStr = [movieModel.images objectForKey:@"large"];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    _image = [UIImage sd_imageWithData:imageData];
    
    [self.firstBtn setBackgroundImage:_image forState:UIControlStateNormal];
    
    self.secondBtn.movieModel = movieModel;
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
